﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Repositories;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Controllers
{
    public class CommentController : Controller
    {
        /// <summary>
        /// Returns a comment, finds it by ID
        /// Precondition:
        /// Postcondition: The comment is returned
        /// Author: Bjarmi
        /// Date: 11.5
        /// </summary>
        public ActionResult getCommentByID(int commentID)
        {
            CommentRepository rep = new CommentRepository();
            AccountRepository uRep = new AccountRepository();
            string userID = Request.Cookies["userInfo"].Values["userID"];
            var comment = rep.getCommentByID(commentID);
            var returnObject = new { UserName = uRep.getUserNameByID(userID), commentID = commentID, commentText = comment.content };
            return Json(returnObject, JsonRequestBehavior.AllowGet);//todo: klára client side ajax
        }

        /// <summary>
        /// Adds a comment to the database
        /// Precondition: The comment has been entered into the appropriate field
        /// Postcondition: The comment is entered into the database. 
        /// Author: Bjarmi
        /// Date: 11.5
        /// </summary>
        public ActionResult addComment(string navString, int ID, string commentText)
        {
            string userID = Request.Cookies["userInfo"].Values["userID"];
            switch (navString)
            {
                case "course":
                    return RedirectToAction("addCourseComment", new { courseID = ID,userID = userID,commentText = commentText });
                case "ass":
                    return RedirectToAction("addAssComment", new { assID = ID, userID = userID, commentText = commentText });
                case "material":
                    return RedirectToAction("addMaterialComment", new { materialID = ID, userID = userID, commentText = commentText });
                case "lecture":
                    return RedirectToAction("addLectureComment", new { lectureID = ID, userID = userID, commentText = commentText });

                default: break;
            }
            return View();
        }

        /// <summary>
        /// Adds comment to course. 
        /// Precondition: The comment has been entered into the form for the course.
        /// Postcondition: The comment has been added to the database. 
        /// Author: Bjarmi
        /// Date: 11.5
        /// </summary>
        public ActionResult addCourseComment(int courseID, string userID, string commentText)
        {
            CommentRepository rep = new CommentRepository();
            Comment c = new Comment();
            c.content = commentText;
            c.authorID = userID;
            int commentID = rep.addCourseComment(courseID, c);
            return RedirectToAction("getCommentByID", new { commentID = commentID });
        }

        /// <summary>
        /// Adds comment to assignment
        /// Precondition: The comment has been entered into the form.
        /// Postcondition: The comment is added to the database
        /// Author: Bjarmi
        /// Date: 11.5
        /// </summary>
        public ActionResult addAssComment(int assID, string userID, string commentText)
        {
            CommentRepository rep = new CommentRepository();
            Comment c = new Comment();
            c.content = commentText;
            c.authorID = userID;
            int commentID = rep.addAssComment(assID, c);
            return RedirectToAction("getCommentByID", new { commentID = commentID });
        }

        /// <summary>
        /// Adds comment to material
        /// Precondition: the comment has been entered into the add comment form
        /// Postcondition: The comment has been added to the database. 
        /// Author: Bjarmi
        /// Date: 11.5 
        /// </summary>
        public ActionResult addMaterialComment(int materialID, string userID, string commentText)
        {
            CommentRepository rep = new CommentRepository();
            Comment c = new Comment();
            c.content = commentText;
            c.authorID = userID;
            int commentID = rep.addMaterialComment(materialID, c);
            return RedirectToAction("getCommentByID", new { commentID = commentID });
        }

        /// <summary>
        /// Adds comment to lecture
        /// Precondition: the comment has been entered into the add comment form
        /// Postcondition: The comment has been added to the database. 
        /// Author: Bjarmi
        /// Date: 11.5 
        /// </summary>
        public ActionResult addLectureComment(int lectureID, string userID, string commentText)
        {
            CommentRepository rep = new CommentRepository();
            Comment c = new Comment();
            c.content = commentText;
            c.authorID = userID;
            int commentID = rep.addLectureComment(lectureID, c);
            return RedirectToAction("getCommentByID", new { commentID = commentID });
        }


        /// <summary>
        /// Loads all comments
        /// Precondition: 
        /// Postcondition: All comments have been loaded
        /// </summary>
        public ActionResult loadComments(string navString, int ID)
        {
            switch(navString)
            {
                case "course":
                        return RedirectToAction("getCourseComments", new { courseID = ID });
                case "ass":
                        return RedirectToAction("getAssComments", new { assID = ID });
                case "material":
                        return RedirectToAction("getMaterialComments", new { materialID = ID });
                case "lecture":
                        return RedirectToAction("getLectureComments", new { lectureID = ID });

                default: break;
            }
            return View();
        }

        /// <summary>
        /// Gets comments for course
        /// Precondition: 
        /// Postcondition: All comments for the course have been loaded. 
        /// </summary>
        public ActionResult getCourseComments(int courseID)
        {
            CommentRepository rep = new CommentRepository();
            AccountRepository Urep = new AccountRepository();
            var commentList = (from c in rep.getCourseComment(courseID)
                               select new
                               {
                                   UserName = Urep.getUserNameByID(c.authorID),
                                   commentID = c.ID,
                                   commentText = c.content
                               });
            return Json(commentList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets comments for assignment
        /// Precondition: 
        /// Postcondition: All comments for the assignment have been loaded. 
        /// </summary>
        public ActionResult getAssComments(int assID)
        {
            CommentRepository rep = new CommentRepository();
            AccountRepository Urep = new AccountRepository();
            var commentList = (from c in rep.getAssComments(assID)
                               select new
                               {
                                   UserName = Urep.getUserNameByID(c.authorID),
                                   commentID = c.ID,
                                   commentText = c.content
                               });
            return Json(commentList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets comments for material
        /// Precondition: 
        /// Postcondition: All comments for the material have been loaded. 
        /// </summary>
        public ActionResult getMaterialComments(int materialID)
        {
            CommentRepository rep = new CommentRepository();
            AccountRepository Urep = new AccountRepository();
            var commentList = (from c in rep.getMaterialComments(materialID)
                               select new
                               {
                                   UserName = Urep.getUserNameByID(c.authorID),
                                   commentID = c.ID,
                                   commentText = c.content
                               });
            return Json(commentList, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets comments for lecture
        /// Precondition: 
        /// Postcondition: All comments for the lecture have been loaded. 
        /// </summary>
        public ActionResult getLectureComments(int lectureID)
        {
            CommentRepository rep = new CommentRepository();
            AccountRepository Urep = new AccountRepository();
            var commentList = (from c in rep.getLectureComments(lectureID)
                               select new
                               {
                                   UserName = Urep.getUserNameByID(c.authorID),
                                   commentID = c.ID,
                                   commentText = c.content
                               });
            return Json(commentList, JsonRequestBehavior.AllowGet);
        }


        public ActionResult removeComment(int commentID)
        {
            string userID = Request.Cookies["userInfo"].Values["userID"];
            AccountRepository uRep = new AccountRepository();

            if (uRep.isTeacher(userID))
            {
                CommentRepository rep = new CommentRepository();
                try
                {
                    rep.removeComment(commentID);
                }
                catch (Exception)
                {
                    return Json(false, JsonRequestBehavior.AllowGet);
                }
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }
    }
}
